<?php
namespace app\bis\controller;
use think\Controller;

class Index extends Base
{
    public function index()
    {
        $username = session('bisAccount.username', '', 'bis');
        $this->assign('username', $username);
        return $this->fetch();
    }
    
    public function welcome()
    {
        $merchant_id = session('bisAccount.merchant_id', '', 'bis');//商户号
        
        $year = date("Y",time());//当前年
        $this_year_start = strtotime($year."0101");//今年1月1日
        $last_year_start = strtotime(($year-1)."0101");//去年1月1日
        
        //统计表
        if(request()->isAjax()){
            //公司名
            $company = db('pay_bis') -> where('merchant_id',$merchant_id) ->value('name');

            //获取该商户当年数据
            $orderlist = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('status',2) -> where('create_time','>=',$this_year_start) -> select();

            $m = date("n",time());//当前月（数值）
            $months = array_slice([1,2,3,4,5,6,7,8,9,10,11,12], 0, $m);
            $categories = array_slice(['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], 0, $m);

            //订单统计
            $totalMoney = 0;
            $totalNum = 0;
            $num = $money = array_slice([0,0,0,0,0,0,0,0,0,0,0,0], 0, $m);
            foreach ($orderlist as $value) {
                foreach ($months as $v) {
                    if(date("n",$value['create_time']) == $v) {
                        $num[$v-1]++;
                        $money[$v-1] += $value['pay_amount']/100;
                    }
                }
            }
        
            $data = [
                'categories' => $categories,
                'num' => $num,
                'money' => $money,
                'company' => $company.'  '.date('Y-m-d H:i:s'),
                'year' => $year
            ];
            return json($data);
        }
        
        //账户余额
        $amount = db('pay_bis') -> where('merchant_id',$merchant_id) ->value('amount');
        //当天订单额
        $todayMoney = db('pay_bis_order') -> where('merchant_id',$merchant_id)
                ->where('create_time','between',[strtotime(date('Y-m-d')), time()])
                ->sum('pay_amount');
        //当天订单额-成交
        $tdyPayAmount = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('status',2)
                ->where('create_time','between',[strtotime(date('Y-m-d')), time()])
                ->sum('pay_amount');
        //昨天订单额
        $yesterdayMoney = db('pay_bis_order') -> where('merchant_id',$merchant_id)
                ->where('create_time','between',[strtotime(date('Y-m-d', strtotime('-1days'))),strtotime(date('Y-m-d'))-1])
                ->sum('pay_amount');
        //昨天订单额-成交
        $ydyPayAmount = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('status',2)
                ->where('create_time','between',[strtotime(date('Y-m-d', strtotime('-1days'))),strtotime(date('Y-m-d'))-1])
                ->sum('pay_amount');
        //日收入增长
        $dailyIncrease = $this -> _getPercent($todayMoney-$yesterdayMoney,$yesterdayMoney,1);
        
        //当天订单量
        $todayNum = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('create_time','between',[strtotime(date('Y-m-d')), time()]) -> count();
        //当天订单量-成交
        $tdyNum = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('status',2) -> where('create_time','between',[strtotime(date('Y-m-d')), time()]) -> count();
        //昨天订单量
        $yesterdayNum = db('pay_bis_order') -> where('merchant_id',$merchant_id) -> where('create_time','between',[strtotime(date('Y-m-d', strtotime('-1days'))),strtotime(date('Y-m-d'))-1]) -> count();
        //日订单量增长
        $annualIncrease = $this -> _getPercent($todayNum-$yesterdayNum,$yesterdayNum,1);
        
        $this->assign('amount', $amount/100);
        $this->assign('todayMoney', $todayMoney/100);
        $this->assign('tdyPayAmount', $tdyPayAmount/100);
        $this->assign('dailyIncrease', $dailyIncrease);
        $this->assign('todayNum', $todayNum);
        $this->assign('tdyNum', $tdyNum);
        $this->assign('annualIncrease', $annualIncrease);
        return $this->fetch();
    }
    
    public function test()
    {
        \phpmailer\Email::send('15907539201@163.com', '123', '123');  // 线上关闭 发送邮件服务
    }
    
    public function _getPercent($dividend, $divisor, $isPercent = 0)
    {
        $percent = 0;
        if (!empty($dividend) && !empty($divisor)) {
            $percent = round($dividend / $divisor, 2);
        }
        if ($isPercent == 1) {
            return ($percent * 100) . '%';
        }
        return $percent;
    }

}
